package com.ffcs.cn.action.base.checkbox;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.TreeComboBox;
import com.supermap.desktop.core.ui.controls.CommonObjectToString;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import org.pushingpixels.substance.api.renderers.SubstanceDefaultTreeCellRenderer;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;

public class DialogTreeComboBoxMultiSelect extends SmDialog {

	private JTree tree;
	private JLabel label;
	private TreeComboBox treeComboBox;

	public DialogTreeComboBoxMultiSelect() {
		//设置标题
		setTitle("TreeComboBox 多选示例");
		//设置初始化窗口大小
		Dimension dimension = new Dimension(300, 400);
		setSize(dimension);
		//设置最小窗口
		setMinimumSize(dimension);
		//初始化使用控件
		initComponents();
		//初始化布局，并添加控件
		initLayout();
	}

	private void initComponents() {
		//创建节点
		DefaultMutableTreeNode workspaceNode = new DefaultMutableTreeNode(new ComboBoxDataItem("工作空间"));

		DefaultMutableTreeNode datasourceNode = new DefaultMutableTreeNode(new ComboBoxDataItem("数据源"));
		//添加子节点
		datasourceNode.add(new DefaultMutableTreeNode(new ComboBoxDataItem("DataSource1")));
		datasourceNode.add(new DefaultMutableTreeNode(new ComboBoxDataItem("DataSource2")));
		datasourceNode.add(new DefaultMutableTreeNode(new ComboBoxDataItem("DataSource3")));

		DefaultMutableTreeNode mapNode = new DefaultMutableTreeNode(new ComboBoxDataItem("地图"));
		mapNode.add(new DefaultMutableTreeNode(new ComboBoxDataItem("Map1")));
		mapNode.add(new DefaultMutableTreeNode(new ComboBoxDataItem("Map2")));
		mapNode.add(new DefaultMutableTreeNode(new ComboBoxDataItem("Map3")));

		DefaultMutableTreeNode layoutNode = new DefaultMutableTreeNode(new ComboBoxDataItem("布局"));
		layoutNode.add(new DefaultMutableTreeNode(new ComboBoxDataItem("Layout1")));
		DefaultMutableTreeNode defaultMutableTreeNodeLayout2 = new DefaultMutableTreeNode(new ComboBoxDataItem("Layout2"));
		layoutNode.add(defaultMutableTreeNodeLayout2);
		layoutNode.add(new DefaultMutableTreeNode(new ComboBoxDataItem("Layout3")));

		workspaceNode.add(datasourceNode);
		workspaceNode.add(mapNode);
		workspaceNode.add(layoutNode);

		//创建树
		this.tree = new JTree(workspaceNode);
		//给根节点显示折叠按钮
		this.tree.setShowsRootHandles(true);
		this.label = new JLabel("按下Shift或Ctrl多选:");
		// 创建树ComboBox，并设置树
		this.treeComboBox = new TreeComboBox(this.tree);
		// 树comboBox开启支持多选
		this.treeComboBox.setSupportMultiSelection(true);
		// 树comboBox 设置自定义的cellRender
		this.tree.setCellRenderer(new TreeCellMultiSelectRenderer());
		// 设置树ComboBox默认选中节点
		this.treeComboBox.setSelectedItem(new TreePath(defaultMutableTreeNodeLayout2.getPath()));
		// 从treeComboBox获取多选的数据
		this.treeComboBox.getSelectedItems();
	}

	private void initLayout(){
		//设置布局为 GridBagLayout 布局
		this.setLayout(new GridBagLayout());

		this.add(this.label, new GridBagConstraintsHelper(0, 0, 1, 1).setAnchor(GridBagConstraints.WEST).setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0));
		this.add(this.treeComboBox, new GridBagConstraintsHelper(1, 0, 1, 1).setAnchor(GridBagConstraints.EAST).setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setWeight(1, 0).setFill(GridBagConstraints.HORIZONTAL));

		this.add(new JPanel(), new GridBagConstraintsHelper(0, 1, 2, 1).setInsets(0, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP).setFill(GridBagConstraints.BOTH).setWeight(1, 1));
	}

	/**
	 *TreeCellMultiSelectRenderer 继承SubstanceDefaultTreeCellRenderer是必须的，方能渲染数据，但这里实现了CommonObjectToString接口，是因为多选后，comboBox的框里需要显示多选的数据，
	 * 需要将数据转为显示的文本，所以就得实现CommonObjectToString接口，如果不是先，就得自行实现comboBox的Editor（参见DialogTreeComboBoxMultiSelectClose），TreeComboBox框里要显示的数据是DefaultMutableTreeNode，
	 * 所以CommonObjectToString泛型需要指定为DefaultMutableTreeNode，然后从DefaultMutableTreeNode获取userObject转成自己定义的数据，并返回显示的文本
	 */
	private class TreeCellMultiSelectRenderer extends SubstanceDefaultTreeCellRenderer implements CommonObjectToString{

		@Override
		public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
			super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
			DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) value;
			ComboBoxDataItem comboBoxDataItem = (ComboBoxDataItem) defaultMutableTreeNode.getUserObject();
			setText(comboBoxDataItem.getValue());
			return this;
		}

		@Override
		public String convertToString(Object object) {
			DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode) object;
			ComboBoxDataItem comboBoxDataItem = (ComboBoxDataItem) defaultMutableTreeNode.getUserObject();
			return comboBoxDataItem.getValue();
		}
	}
}
